package net.sf.odinms.client.messages;

import java.sql.PreparedStatement;
import net.sf.odinms.tools.Pair;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.database.DatabaseConnection;

public class CommandProcessor implements CommandProcessorMBean {

    private static CommandProcessor instance = new CommandProcessor();
    private static java.util.List<Pair<MapleCharacter, String>> gmlog = new java.util.LinkedList<Pair<MapleCharacter, String>>();
    private static Runnable persister;


    static {
        persister = new PersistingTask();
        net.sf.odinms.server.TimerManager.getInstance().register(persister, 62000);
    }

    public static CommandProcessor getInstance() {
        return instance;
    }

    private CommandProcessor() {
    }

    private static class PersistingTask implements Runnable {

        @Override
        public void run() {
            synchronized (gmlog) {
                try {
                    PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("INSERT INTO gmlog (cid, command) VALUES (?, ?)");
                    for (Pair<MapleCharacter, String> logentry : gmlog) {
                        ps.setInt(1, logentry.getLeft().getId());
                        ps.setString(2, logentry.getRight());
                        ps.executeUpdate();
                    }
                    ps.close();
                } catch (Exception e) {
                }
                gmlog.clear();
            }
        }
    }

    public static void registerMBean() {
        try {
            java.lang.management.ManagementFactory.getPlatformMBeanServer().registerMBean(instance, new javax.management.ObjectName("net.sf.odinms.client.messages:name=CommandProcessor"));
        } catch (Exception e) {
        }
    }

    public static boolean processCommandInternal(MapleClient c, MessageCallback mc, String line) {
        if (line.charAt(0) == '!' && c.getPlayer().getGMLevel() > 0) {
            //if (c.getPlayer().isLogged()) {
                synchronized (gmlog) {
                    gmlog.add(new Pair<MapleCharacter, String>(c.getPlayer(), line));
                }
            //}
            if (c.getPlayer().getGMLevel() > 0) { //实习GM
                if (InternCommand.executeInternCommand(c, mc, line)) {
                    return true;
                }
            }
            if (c.getPlayer().getGMLevel() > 1) { //GM
                if (GMCommand.executeCommand(c, mc, line)) {
                    return true;
                }
            }
            if (c.getPlayer().getGMLevel() > 2) { //系统管理员
                if (AdminCommand.executeCommand(c, mc, line)) {
                    return true;
                }
            }
            //return GMCommand.executeCommand(c, mc, line) || (c.getPlayer().gmLevel() > 1 && AdminCommand.executeCommand(c, mc, line)) || true;
        } else if (line.charAt(0) == '@') {
            return PlayerCommand.executeCommand(c, mc, line);
        }
        return false;
    }
}